package cn.tedu.sp02.item.service.controller;

import cn.tedu.sp01.pojo.Item;
import cn.tedu.sp01.service.ItemService;
import cn.tedu.web.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
public class ItemController {
    //注入servies
    @Autowired
    private ItemService itemService;

    //注入端口号
    //是为了后面测试集群服务器负载均衡
    @Value("${server.port}")
    private int port;

    //获取顶戴中的商品列表
    @GetMapping("/{orderId}")
    public JsonResult<List<Item>> getItems(@PathVariable String orderId){
        List<Item> items = itemService.getItems(orderId);
        log.info("获取商品列表，orderId=" +orderId);
        return JsonResult.ok().msg("server.port="+port).data(items);
    }
    //减少商品库存
    @PostMapping("decreaseNumder")

    public JsonResult<?> decreaseNumder(@RequestBody List<Item> items){
        itemService.decreaseNumder(items);
        return JsonResult.ok().msg("减少商品库存成功");
    }
}
